package
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.events.Event;
	
	public class BoxMotion extends Sprite
	{
		//VARIABLES INTERNAS SETEABLES OBLIGATORIAS.
		
			public var clip:Sprite;
		
		//VARIABLES INTERNAS SETEABLES OPCIONALES.
			
			public var limits:Rectangle = new Rectangle(0, 0, 800, 600);
			public var vMin:Number = -10;
			public var vMax:Number = 10;
			
		//VARIABLES INTERNAS.
		
			private var vX:Number;
			private var vY:Number;
			
		//CONSTRUCTOR.
		public function BoxMotion()
		{
			
		}
		
		//INIT.
		public function init():void
		{
			//VELOCIDAD INICIAL AL AZAR.
			vX = (vMax - vMin) * Math.random() + vMin;
			vY = (vMax - vMin) * Math.random() + vMin;
			
			//INICIA EL ENTERFRAME.
			clip.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		//ENTERFRAME.
		private function enterFrameHandler(evt:Event):void
		{
			//MOVIMIENTO.
			clip.x += vX;
			clip.y += vY;
			
			//CONTENCION.
			if (clip.x + clip.width/2 > limits.width)
			{
				clip.x = limits.width - clip.width / 2;
				vX *= -1;
			}
			else if (clip.x - clip.width/2 < 0)
			{
				clip.x = clip.width / 2;
				vX *= -1;
			}
			if (clip.y + clip.height/2 > limits.height)
			{
				clip.y = limits.height - clip.height / 2;
				vY *= -1;
			}
			else if (clip.y - clip.height/2 < 0)
			{
				clip.y = clip.height / 2;
				vY *= -1;
			}
		}
	}
}